package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Entity.ExamReport;
import com.weixin.project.Entity.PracticeHistory;
import com.weixin.project.Entity.Question;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.PracticeService;
import com.weixin.project.Service.QuestionService;
import com.weixin.project.Service.ExamService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 模拟考试
 */
@RestController
public class ExamController {
    @Autowired
    ExamService examService;
    @Autowired
    UserService userService;
    @Autowired
    QuestionService questionService;
    @Autowired
    PracticeService practiceService;

    @GetMapping("/simulate/questions")
    JSONObject getExamPaper(){
        List<Question> questions = examService.generateExamPaper();
        JSONArray array = new JSONArray();
        for(Question question:questions){
            JSONObject obj = new JSONObject();
            obj.put("stem", question.getStem());
            obj.put("explain", question.getExplanation());
            obj.put("score", question.getIntScore());
            obj.put("difficulty", question.getDifficulty());
            obj.put("type_id", question.getTypeId());
            obj.put("id", question.getId());
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("questions", array);
        return ResponseUtil.success("", ret);
    }

    /**
     * 提交模拟考试试卷
     * @param request request
     * @param params 请求体 格式为jsonArray:[{"questionId",[answer, ...]}]
     * @return response
     */
    @PostMapping("/exam/submit")
    JSONObject handInExamPaper(HttpServletRequest request,
                               @RequestBody JSONObject params){
        User user = userService.findCurrentUser(request);
        JSONArray myAnswers = params.getJSONArray("my_select");
        JSONArray array = new JSONArray();
        Integer time = params.getInteger("time");
        ExamReport examReport = examService.handInPaper(myAnswers, time, user.getId());
        System.out.println("judge results:" + examReport.getJudgeResult().toString());
        System.out.println("right flags:" + examReport.getRightFlags().toString());
        for (int i = 0; i < examReport.getQuestionCount(); i++) {
            JSONObject obj = new JSONObject();
            obj.put("is_right", examReport.getJudgeResult().getBoolean(i));
            obj.put("answers", examReport.getRightFlags().getJSONArray(i));
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("answers", array);
        ret.put("correct", examReport.getCorrect());
        ret.put("total_score", examReport.getScore());
        return ResponseUtil.success("", ret);
    }
}
